﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2017/10/26 15:40:41------------------------------------------
----------------------------------------------------------------------------------*/


using Donal.Xms.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class coupons_allotment_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string keywords = string.Empty;
        protected string status = string.Empty;
        protected string isUsed = string.Empty;
        protected string couponsType = string.Empty;
        protected string start_time = string.Empty;
        protected string end_time = string.Empty;
        protected string ot = string.Empty;
        private BLL.company companyBll;
        protected void Page_Load(object sender, EventArgs e)
        {
            companyBll = new BLL.company();
            this.keywords = DTRequest.GetQueryString("keywords");
            this.status = DTRequest.GetQueryString("status");
            this.isUsed = DTRequest.GetQueryString("isUsed");
            this.couponsType = DTRequest.GetQueryString("couponsType");
            this.start_time = DTRequest.GetQueryString("start_time");
            this.end_time = DTRequest.GetQueryString("end_time");
            this.ot = DTRequest.GetQueryString("ot");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("common_coupons_allotment", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0" + CombSqlTxt(keywords), "id desc");
            }
        }

        #region 获取会员名称

        public string GetCompanyName(string user_id, string fileName)
        {
            return companyBll.GetValue(int.Parse(user_id), fileName);
        }
        #endregion

        #region 获取到期状态

        public string GetOverStatus(string over_time)
        {
            if (over_time == "")
            {
                return "永不过期";
            }
            else
            {
                if (DateTime.Parse(over_time) < DateTime.Now)
                {
                    return "已过期";
                }
                else
                {
                    TimeSpan ts = DateTime.Parse(over_time) - DateTime.Now;
                    int days = ts.Days;
                    if (days == 0)
                    {
                        return "今天到期";
                    }
                    else
                    {
                        return "还差 " + days + " 天到期";
                    }
                }
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            ddlStatus.SelectedValue = status;
            ddlCouponsType.SelectedValue = couponsType;
            ddlIsUsed.SelectedValue = isUsed;
            this.txtStartTime.Text = this.start_time;
            this.txtEndTime.Text = this.end_time;
            this.ddlOvertime.SelectedValue = ot;

            BLL.coupons_allotment bll = new BLL.coupons_allotment();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("coupons_allotment_list.aspx", "status={0}&isUsed={1}&couponsType={2}&keywords={3}&ot={4}&start_time={5}&end_time={6}&page={7}",
                status, isUsed, couponsType, this.keywords, this.ot, this.start_time, this.end_time, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords + "%' or user_id in(select id from dt_company where company  like  '%" + _keywords + "%' ) or user_id in(select id from dt_company where erpCode  like  '%" + _keywords + "%' )) ");
            }

            if (status != "")
            {
                switch (status)
                {
                    case "z":
                        strTemp.Append("  and coupon_ids is not null and coupon_ids!=''");
                        break;
                    case "j":
                        strTemp.Append("  and (coupon_ids is null or coupon_ids='')");
                        break;
                }
            }
            if (isUsed != "")
            {
                switch (isUsed)
                {
                    case "u":
                        strTemp.Append(" and (order_no is not null and order_no!='')");
                        break;
                    case "n":
                        strTemp.Append(" and (order_no is null or order_no='')");
                        break;
                }
            }
            if (ot != "")
            {
                switch (ot)
                {
                    case "x":
                        strTemp.Append(" and CONVERT(datetime,over_time)< GETDATE() ");
                        break;
                    case "y":
                        strTemp.Append(" and (over_time ='' or CONVERT(datetime,over_time)>= GETDATE()) ");
                        break;
                }
            }

            if (couponsType != "")
            {
                switch (couponsType)
                {
                    case "d":
                        strTemp.Append(" and  superposition=1");
                        break;
                    case "m":
                        strTemp.Append(" and  superposition=0");
                        break;

                }
            }
            if (!string.IsNullOrEmpty(start_time))
            {
                strTemp.Append(" and datediff(d,allotment_time,'" + start_time + "')<=0");
            }

            if (!string.IsNullOrEmpty(end_time))
            {
                strTemp.Append(" and datediff(d,allotment_time,'" + end_time + "')>=0");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("coupons_allotment_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("coupons_allotment_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            redirect();
        }

        #region 删除

        /// <summary>
        /// 删除优惠券
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLocked_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("common_coupons_allotment", DTEnums.ActionEnum.Audit.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.coupons_allotment bll = new BLL.coupons_allotment();
            Model.coupons_allotment coupons_Allotment = new Model.coupons_allotment();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");

                string un = ((HiddenField)rptList.Items[i].FindControl("hidUserName")).Value;
                string co = ((HiddenField)rptList.Items[i].FindControl("hidContents")).Value;

                if (cb.Checked)
                {
                    coupons_Allotment = bll.GetModel(id);

                    if (coupons_Allotment != null && bll.Delete(id))
                    {
                        sucCount += 1;

                        AddAdminLog(DTEnums.ActionEnum.Audit.ToString(), "成功删除分配给会员“" + un + "”的优惠券“" + co + "” " + JsonConvert.SerializeObject(coupons_Allotment)); //记录日志
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }

            JscriptMsg("成功删除分配给会员的优惠券" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("coupons_allotment_list.aspx",
                "status={0}&isUsed={1}&couponsType={2}&keywords={3}&ot={4}&start_time={5}&end_time={6}",
                status, isUsed, couponsType, keywords, ot, txtStartTime.Text, txtEndTime.Text));
        }



        #endregion

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlIsUsed_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlCouponsFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlCouponsType_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlOvertime_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        private void redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("coupons_allotment_list.aspx", "status={0}&isUsed={1}&couponsType={2}&keywords={3}&ot={4}&start_time={5}&end_time={6}",
                ddlStatus.SelectedValue, ddlIsUsed.SelectedValue, ddlCouponsType.SelectedValue, txtKeywords.Text, ddlOvertime.SelectedValue, txtStartTime.Text, txtEndTime.Text));
        }

        protected void btnBatAllotment_Click(object sender, EventArgs e)
        {

            #region 2022-08-10 每个客户派发一张“大穗画册免费券”
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("'105853',");
            stringBuilder.Append("'102777',");
            stringBuilder.Append("'108383',");
            stringBuilder.Append("'107945',");
            stringBuilder.Append("'SC005311',");
            stringBuilder.Append("'SC001273',");
            stringBuilder.Append("'SC064980',");
            stringBuilder.Append("'105477',");
            stringBuilder.Append("'SC049349',");
            stringBuilder.Append("'SC002115',");
            stringBuilder.Append("'107633',");
            stringBuilder.Append("'SC009395',");
            stringBuilder.Append("'105246',");
            stringBuilder.Append("'SC000795',");
            stringBuilder.Append("'SC045415',");
            stringBuilder.Append("'SC009145',");
            stringBuilder.Append("'SC064704',");
            stringBuilder.Append("'SC063746',");
            stringBuilder.Append("'SC009895',");
            stringBuilder.Append("'SC058608',");
            stringBuilder.Append("'SC004997',");
            stringBuilder.Append("'SC007425',");
            stringBuilder.Append("'SC008872',");
            stringBuilder.Append("'SC020546',");
            stringBuilder.Append("'SC046604',");
            stringBuilder.Append("'SC049810',");
            stringBuilder.Append("'SC069833',");
            stringBuilder.Append("'107321',");
            stringBuilder.Append("'106646',");
            stringBuilder.Append("'SC083911',");
            stringBuilder.Append("'SC071934',");
            stringBuilder.Append("'102836',");
            stringBuilder.Append("'SC071774',");
            stringBuilder.Append("'102481',");
            stringBuilder.Append("'100304',");
            stringBuilder.Append("'SC007266',");
            stringBuilder.Append("'108834',");
            stringBuilder.Append("'SC048569',");
            stringBuilder.Append("'101366',");
            stringBuilder.Append("'108103',");
            stringBuilder.Append("'102856',");
            stringBuilder.Append("'SC002581',");
            stringBuilder.Append("'SC063546',");
            stringBuilder.Append("'107697',");
            stringBuilder.Append("'104713',");
            stringBuilder.Append("'SC072832',");
            stringBuilder.Append("'107190',");
            stringBuilder.Append("'100654',");
            stringBuilder.Append("'108353',");
            stringBuilder.Append("'104020',");
            stringBuilder.Append("'SC001768',");
            stringBuilder.Append("'SC045361',");
            stringBuilder.Append("'107537',");
            stringBuilder.Append("'108523',");
            stringBuilder.Append("'SC084810',");
            stringBuilder.Append("'108017',");
            stringBuilder.Append("'SC074610',");
            stringBuilder.Append("'SC051019',");
            stringBuilder.Append("'SC006407',");
            stringBuilder.Append("'SC064031',");
            stringBuilder.Append("'SC071084',");
            stringBuilder.Append("'SC006513',");
            stringBuilder.Append("'SC006448',");
            stringBuilder.Append("'SC062521',");
            stringBuilder.Append("'SC001273',");
            stringBuilder.Append("'SC002743',");
            stringBuilder.Append("'108795',");
            stringBuilder.Append("'106534',");
            stringBuilder.Append("'SC008695',");
            stringBuilder.Append("'SC005439',");
            stringBuilder.Append("'SC064399',");
            stringBuilder.Append("'104775',");
            stringBuilder.Append("'108364'");

            #endregion


            Dictionary<int, int> dic_coupons_quantity = new Dictionary<int, int>();
            dic_coupons_quantity.Add(86, 1); //大穗画册免费券


            if (new BLL.coupons_allotment().allotmentByErpCodes(stringBuilder.ToString(), dic_coupons_quantity, GetAdminInfo(), DTRequest.GetIP(), DTEnums.ActionEnum.Audit.ToString()))
            {
                Utils.WriteCookie("coupons_allotment_user_ids", "");
                JscriptMsg("指定会员分配优惠券成功！", "coupons_allotment_list.aspx");
            }
            else
            {
                JscriptMsg("指定会员分配优惠券失败！", "");
            }
        }
    }
}

